<template>
  <div class="all">
    <!-- 榜单 -->
    <div class="title">榜单</div>
    <div class="content">
      <div v-for="item in topList" :key="item.id" class="contentBox">
        <el-card shadow="always" :body-style="{ padding: '0px' }">
          <div class="count">
            <div>{{ item.updateFrequency }}</div>
            <div>
              <i class="el-icon-video-play"></i>{{
                  item.playCount >= 10000
                    ? (item.playCount / 10000).toFixed(0) + "万"
                    : item.playCount
              }}
            </div>
          </div>
          <el-image :src="item.coverImgUrl" @click="getItem(item.id)" lazy>
            <div slot="placeholder" class="image-slot">
              <el-image @click="getItem(item.id)"
                src="https://s4.music.126.net/style/web2/img/default/default_album.jpg" lazy></el-image>
            </div>
          </el-image>
          <div style="padding: 14px">
            <el-tooltip class="item" effect="dark" :content="item.description" placement="top">
              <div class="desc">
                {{ item.description === null ? item.name : item.description }}
              </div>
            </el-tooltip>
          </div>
        </el-card>
      </div>
    </div>
    <div class="loading" v-if="show === true">
      <i class="el-icon-loading"></i>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions } from 'vuex';

export default {
  mounted() {
    this.getTopList();
  },
  beforeRouteLeave(to, from, next) {
    this.show = true
    next()
  },
  data() {
    return {
      topList: null,
      topArtist: null,
      topArtistData: null,
      number: 8,
      count: 0,
      show: false,
    };
  },
  methods: {
    ...mapActions(['saveSongList', 'clickPlay']),
    // 获取排行榜
    async getTopList() {
      this.topList = null;
      this.show = true;
      this.$api.topList().then((ret) => {
        this.topList = ret.list;
        this.show = false;
      });
    },
    more() {
      this.number += 8;
      this.topArtistData = [];
      this.topArtistData = this.topArtist.slice(this.number, this.number + 8);
      if (this.number > this.number) {
        this.topArtistData = [];
        this.number = 8;
        this.topArtistData = this.topArtist.slice(0, this.number);
      }
    },
    // 跳转到歌单明细
    async getItem(e) {
      this.show = true;
      this.$api.getPlayList({ id: e, timestamp: this.$utils.timestamp(), cookie: localStorage.getItem('cookie') }).then((ret) => {
        if (ret.code === 200) {
          this.saveSongList({ songList: ret.playlist }); // 获取到的明细存入VUEX
          this.$router.push(`/songList?id=${e}`);
          this.clickPlay({ ifPlay: true });
        } else {
          this.$message.error('超时获取失败!!!');
        }
      });
    },
  },
};
</script>

<style scoped>
.all {
  position: relative;
}

.title {
  margin-top: 10px;
  margin-bottom: 10px;
  font-size: 24px;
  color: rgb(236, 65, 65);
}

.content {
  display: flex;
  flex-wrap: wrap;
  margin-bottom: 40px;
}

.el-card {
  margin-bottom: 10px;
  cursor: pointer;
  width: 200px;
  position: relative;
}

.contentBox {
  margin-right: 5px;
}

.el-image {
  width: 100%;
}

.count {
  position: absolute;
  z-index: 10;
  width: 100%;
  text-align: right;
  color: #fff;
  background-image: linear-gradient(#080808, #aca5a538);
  display: flex;
  justify-content: space-between;
}

.desc {
  width: 100%;
  height: 20px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.name {
  margin-top: 10px;
  margin-bottom: 10px;
  text-align: center;
}

.loading {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
}

.el-icon-loading {
  position: fixed;
  font-size: 50px;
  left: 50%;
  top: 50%;
  color: #ec4141;
}
</style>
